package cn.cocho.android.mc.comp.button;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.RadioButton;

import cn.cocho.android.mc.R;

/**
 * 自定义单选按钮
 * 
 * @author HAIKANG SONG
 * @time 2013-5-14下午2:44:16
 *        
 */
/**
 * 采用新的方法获取自定义的属性 (Version:2.0)
 * 
 * @author Doraemon
 * @time 2014年9月28日下午2:17:16
 */
public class McRadioButton extends RadioButton {

	private Drawable unCheckedEnabledImg;
	private Drawable unCheckedDisnabledImg;
	private Drawable checkedEnabledImg;
	private Drawable checkedDisnabledImg;
	private LocationImg locationImg = LocationImg.LEFT;// 单选按钮图片的位置

	private int normalColor = 0;
	private int disableColor;
	private int checkedColor;

	private enum LocationImg {
		LEFT, TOP, RIGHT, BOTTOM, BACKGROUND
	}

	private AttributeSet attrs;
	private Context context;
	private State currentState = State.UNCHECKED_ENABLE;

	private enum State {
		UNCHECKED_ENABLE, UNCHECKED_DISABLE, CHECKED_ENABLE, CHECKED_DISABLE
	};

	public McRadioButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.attrs = attrs;
		this.context = context;
		initColorStates();
		initRadioButton();
	}

	/**
	 * 初始化单选按钮
	 * 
	 * @author HAIKANG SONG
	 * @time 2013-5-14下午2:20:44
	 */
	public void initRadioButton() {
		initLocationImg();
		unCheckedEnabledImg = getDrawable(checkedEnabledImg, R.styleable.McRadioButton_radioButton_uncheckedEnabledImg);

		checkedEnabledImg = getDrawable(checkedEnabledImg, R.styleable.McRadioButton_radioButton_checkedEnabledImg);

		checkedDisnabledImg = getDrawable(checkedDisnabledImg, R.styleable.McRadioButton_radioButton_checkedDisabledImg);

		unCheckedDisnabledImg = getDrawable(unCheckedDisnabledImg, R.styleable.McRadioButton_radioButton_uncheckedDisnableImg);

		updateState();
	}

	private void initLocationImg() {
		// 定义成字符串类型是最好的，但是安卓的自定义组件的枚举值只能为数字
		TypedArray tdarr = context.obtainStyledAttributes(attrs, R.styleable.McRadioButton);
		int location = tdarr.getInteger(R.styleable.McRadioButton_radioButton_locationImg, 0);
		if (location == 0) {
			locationImg = LocationImg.LEFT;
		} else if (location == 1) {
			locationImg = LocationImg.TOP;
		} else if (location == 2) {
			locationImg = LocationImg.RIGHT;
		} else if (location == 3) {
			locationImg = LocationImg.BOTTOM;
		} else if (location == 4) {
			locationImg = LocationImg.BACKGROUND;
		}
		tdarr.recycle();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		super.onTouchEvent(event);
		if (!isEnabled()) {
			return false;
		}

		if (!isChecked()) {
			setChecked(true);
		}
		return false;
	}

	/**
	 * 改变单选按钮当前的状态
	 * 
	 * @author HAIKANG SONG
	 * @time 2013-5-14下午4:26:05
	 */
	private void updateState() {
		if (!isChecked() && isEnabled()) {
			updateRadioButtonImg(State.UNCHECKED_ENABLE, unCheckedEnabledImg);
			setTextColor(normalColor);
		} else if (isChecked() && isEnabled()) {
			updateRadioButtonImg(State.CHECKED_ENABLE, checkedEnabledImg);
			setTextColor(checkedColor);
		} else if (!isChecked() && !isEnabled()) {
			updateRadioButtonImg(State.UNCHECKED_DISABLE, unCheckedDisnabledImg);
			setTextColor(disableColor);
		} else if (isChecked() && !isEnabled()) {
			updateRadioButtonImg(State.CHECKED_DISABLE, checkedDisnabledImg);
			setTextColor(disableColor);
		}
	}

	/**
	 * 修改单选按钮的图片
	 * 
	 * @param state
	 *            状态
	 * @param img
	 *            图片对象
	 * @author HAIKANG SONG
	 * @time 2013-5-14下午10:51:54
	 */
	private void updateRadioButtonImg(State state, Drawable img) {
		if (currentState != state && img != null) {
			if (locationImg == LocationImg.LEFT) {
				setCompoundDrawablesWithIntrinsicBounds(img, null, null, null);
			} else if (locationImg == LocationImg.TOP) {
				setCompoundDrawablesWithIntrinsicBounds(null, img, null, null);
			} else if (locationImg == LocationImg.RIGHT) {
				setCompoundDrawablesWithIntrinsicBounds(null, null, img, null);
			} else if (locationImg == LocationImg.BOTTOM) {
				setCompoundDrawablesWithIntrinsicBounds(null, null, null, img);
			} else if (locationImg == LocationImg.BACKGROUND) {
				setBackgroundDrawable(img);
			}
			currentState = state;
		}
	}

	/**
	 * 获得指定属性背景图片对象
	 * 
	 * @param drawable
	 *            背景图片对象
	 * @param eventName
	 *            属性名称
	 * @return
	 * @author HAIKANG SONG
	 * @time 2013-5-14下午4:27:26
	 */
	// private Drawable getDrawable(Drawable drawable, String eventName) {
	// if (drawable == null) {
	// Resources resources = getResources();
	// int drawableId = attrs.getAttributeResourceValue(CompUtils.NS_TBC,
	// eventName, 0);// 读取该属性的值
	// if (drawableId > 0) {
	// drawable = resources.getDrawable(drawableId);
	// } else {
	// drawable = unCheckedEnabledImg;
	// }
	// }
	// return drawable;
	// }

	private Drawable getDrawable(Drawable drawable, int index) {
		if (drawable == null) {
			TypedArray tdarr = context.obtainStyledAttributes(attrs, R.styleable.McRadioButton);
			Drawable drawableRes = tdarr.getDrawable(index);
			if (drawableRes != null) {
				drawable = drawableRes;
			} else {
				drawable = unCheckedEnabledImg;
			}
			tdarr.recycle();
		}
		return drawable;
	}

	// private void initColorStates() {
	// Resources resources = getResources();
	// TypedArray attrArray = context.obtainStyledAttributes(attrs,
	// R.styleable.TbcRadioButton);
	//
	// int normalStateId = attrs.getAttributeResourceValue(CompUtils.NS_TBC,
	// TEXT_COLOR, 0);
	// if (normalStateId <= 0) {
	// normalStateId =
	// attrArray.getResourceId(R.styleable.TbcRadioButton_radioButton_text_color,
	// 0);
	// }
	//
	// if (normalStateId > 0) {
	// normalColor = resources.getColor(normalStateId);
	// }
	//
	// int pressedStateId = attrs.getAttributeResourceValue(CompUtils.NS_TBC,
	// CHECKED_TEXT_COLOR, 0);
	// if (pressedStateId <= 0) {
	// pressedStateId =
	// attrArray.getResourceId(R.styleable.TbcRadioButton_radioButton_check_text_color,
	// 0);
	// }
	//
	// if (pressedStateId > 0) {
	// checkedColor = resources.getColor(pressedStateId);
	// } else {
	// checkedColor = normalColor;
	// }
	//
	// int disableStateId = attrs.getAttributeResourceValue(CompUtils.NS_TBC,
	// DISABLE_TEXT_COLOR, 0);
	// if (pressedStateId <= 0) {
	// disableStateId =
	// attrArray.getResourceId(R.styleable.TbcRadioButton_radioButton_disable_text_color,
	// 0);
	// }
	//
	// if (disableStateId > 0) {
	// disableColor = resources.getColor(disableStateId);
	// } else {
	// disableColor = normalColor;
	// }
	// attrArray.recycle();
	// }

	private void initColorStates() {
		TypedArray tdarr = context.obtainStyledAttributes(attrs, R.styleable.McRadioButton);
		normalColor = tdarr.getColor(R.styleable.McRadioButton_radioButton_text_color, 0);

		int checkedColorRes = tdarr.getColor(R.styleable.McRadioButton_radioButton_check_text_color, 0);
		if (checkedColorRes > 0) {
			checkedColor = checkedColorRes;
		} else {
			checkedColor = normalColor;

		}

		int disableColorRes = tdarr.getColor(R.styleable.McRadioButton_radioButton_disable_text_color, 0);
		if (disableColorRes > 0) {
			disableColor = disableColorRes;
		} else {
			disableColor = normalColor;

		}
		tdarr.recycle();
	}

	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		updateState();
	}

	@Override
	public void setChecked(boolean checked) {
		super.setChecked(checked);
		updateState();
	}

	public Drawable getUnCheckedEnabledImg() {
		return unCheckedEnabledImg;
	}

	public void setUnCheckedEnabledImg(Drawable unCheckedEnabledImg) {
		this.unCheckedEnabledImg = unCheckedEnabledImg;
	}

	public Drawable getUnCheckedDisnabledImg() {
		return unCheckedDisnabledImg;
	}

	public void setUnCheckedDisnabledImg(Drawable unCheckedDisnabledImg) {
		this.unCheckedDisnabledImg = unCheckedDisnabledImg;
	}

	public Drawable getCheckedEnabledImg() {
		return checkedEnabledImg;
	}

	public void setCheckedEnabledImg(Drawable checkedEnabledImg) {
		this.checkedEnabledImg = checkedEnabledImg;
	}

	public Drawable getCheckedDisnabledImg() {
		return checkedDisnabledImg;
	}

	public void setCheckedDisnabledImg(Drawable checkedDisnabledImg) {
		this.checkedDisnabledImg = checkedDisnabledImg;
	}

	public LocationImg getLocationImg() {
		return locationImg;
	}

	public void setLocationImg(LocationImg locationImg) {
		this.locationImg = locationImg;
	}

}
